package net.zjitc.servlet;

import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by Administrator on 2017/9/21.
 */
public class AServlet implements Servlet {
    //第一步：定义一个成员变量servletConfig，所有方法都可以用
    private ServletConfig servletConfig=null;
    /**
     * 默认第一次访问的时候出生
     * @param servletConfig
     * @throws ServletException
    */
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        //第二步：初始化时赋值servletConfig
        this.servletConfig=servletConfig;
        System.out.println("出生了");
    }

    @Override
    public ServletConfig getServletConfig() {

        return null;
    }
    /**
     * 每次有请求，都有此方法
     * @param servletRequest
     * @param servletResponse
     * @throws ServletException
     * @throws IOException
    */
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        //第三步：读取servletConfig相关信息，输出到页面
        String value=this.servletConfig.getInitParameter("Ecoding");
        PrintWriter out=servletResponse.getWriter();
        out.write("aServlet....<br>");
        //out.write(value);
        /*out.write("<h1>"+value+"</h1> </br>");
        System.out.println("haha");*/
        ServletContext context=this.servletConfig.getServletContext();
        context.setAttribute("name","zhangsan");
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("死了。。。");
    }
}
